package com.gitee.wsl.ext.collection


inline fun <reified T, reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceMapTo(
    destination: C,
    transform: (T) -> R
): C {
    for (element in this) {
        if (element is T) {
            destination.add(transform(element))
        }
    }
    return destination
}

inline fun <reified R> Collection<*>.filterIsInstanceAnd(predicate: (R) -> Boolean): List<R> {
    if (isEmpty()) return emptyList()
    return filterIsInstanceAndTo(mutableListOf(), predicate)
}

inline fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceAndTo(
    destination: C,
    predicate: (R) -> Boolean
): C {
    for (element in this) {
        if (element is R && predicate(element)) {
            destination.add(element)
        }
    }
    return destination
}
